package com.nineclock.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.system.dto.SysRegisterDTO;
import com.nineclock.system.mapper.SysUserMapper;
import com.nineclock.system.pojo.SysUser;
import com.nineclock.system.service.SmsService;
import com.nineclock.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhangyan
 * @date 2021/3/31 23:29
 */
@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SmsService smsService;

    @Override
    public SysUser queryUser(String username) {

        //校验
        if (StrUtil.isEmpty(username)) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //组装查询条件
        //select * from sys_user where username='小明' or mobile = 'XXXXXX'
        LambdaQueryWrapper<SysUser> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(SysUser::getUsername, username).or().eq(SysUser::getMobile, username);

        //执行查询
        SysUser user = sysUserMapper.selectOne(userWrapper);

        return user;
    }

    /**
     * 校验手机号是否存在
     *
     * @param mobile
     * @return
     */
    @Override
    public Boolean checkExists(String mobile) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getMobile, mobile); //where mobile = ?

        Integer count = sysUserMapper.selectCount(queryWrapper); //查询出有记录即可
        return count == 1;
    }

    /**
     * 新用户注册
     *
     * @param registerDTO
     * @return
     */
    @Override
    public Long regiester(SysRegisterDTO registerDTO) {
        //1. 参数校验
        if (registerDTO == null || StrUtil.isEmpty(registerDTO.getMobile()) || StrUtil.isEmpty(registerDTO.getCheckcode()) || StrUtil.isEmpty(registerDTO.getPassword())) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        /*
           *手机号无需再次判定,如果手机号已经注册,则无法获取验证码
         */
        //2. 校验验证码
        Boolean verifyResult = smsService.verify(registerDTO.getMobile(), SMSConstant.SMS_TYPE_REGISTER, registerDTO.getCheckcode());
        if (!verifyResult) {
            throw new NcException(ResponseEnum.CODE_IMAGE_ERROR);
        }

        //3. 保存用户信息
        SysUser sysUser = new SysUser();
        sysUser.setMobile(registerDTO.getMobile());
        //BCryptPasswordEncoder加密
        sysUser.setPassword(new BCryptPasswordEncoder().encode(registerDTO.getPassword()));

        sysUserMapper.insert(sysUser);

        //4. 返回
        return sysUser.getId(); //id是主键,插入数据后 MybatisPlus会自动将主键返回并赋值给实体类属性
    }
}
